
; -----------------------------------------------------------------------------
; Routine:      stime
; Called By:    menus
; Calls:        wrmess (write message to LCD)
;               wrcmd (write command byte to LCD)
;               bin2as (convert binary to ASCII)
;               wrstr (write string to LCD)
;               mthmax (figger max days in month)
; Gazintas:     None
; Gazoutas:     month,day,year,hours,mins,secs,lhrs (new time)
; Function:     This is the routine to set the UTC time and local time.  It is
;               used for the header of the weather string.  While setting the
;               time, we turn off the time part of the timer (rtchld) so that
;               the time doesn't increment while we are adjusting the time.
;               Local time is so we can reset the 24-hour stats at midnight.
; -----------------------------------------------------------------------------
stime:  lda     #1              ; not 0
        sta     zfill           ; want zero-filling

        lda     #18             ; message #18 (setting time)
        jsr     wrmess          ; write it
        clr     keycnt          ; clear key counter
        inc     rtchld          ; hold off RTC
        lda     #$0e            ; cursor on command
        jsr     wrcmd           ; turn the cursor on
        clr     cntr            ; our field pointer

stlp:   lda     cntr            ; get the field counter
        cmp     #5              ; see if we are at the last field
        bne     stlp14          ; if not lst one, screen 1-4 processing

stlp5:  lda     #20             ; Set Local Hour message
        jsr     wrmess          ; show message
        lda     #$8f            ; the place for the local hour
        sta     msg             ; start of message

        lda     lhrs            ; get the local hours
        sta     divdnd+1        ; place to convert
        clr     divdnd          ; clear MSB
        jsr     bin2as          ; convert it
        lda     atemp+1         ; get 10's
        sta     msg+1           ; store it
        lda     atemp+2         ; get 1's
        sta     msg+2           ; store it
        clr     msg+3           ; EOS flag
        jmp     stmsg           ; go write it


stlp14: lda     #$86            ; the place to write the string to
        sta     msg             ; spot #1
        lda     #'/'            ; date seperator
        sta     msg+3           ; spot #1
        sta     msg+6           ; spot #2
        lda     #' '            ; space between date and time
        sta     msg+9           ; plop it down
        lda     #':'            ; colon between hrs and mins
        sta     msg+12          ; plop it down
        clr     msg+15          ; the EOS chracter

        lda     month           ; get the month
        sta     divdnd+1        ; place to convert
        clr     divdnd          ; clear MSB
        jsr     bin2as          ; convert it
        lda     atemp+1         ; get 10's
        sta     msg+1           ; store it
        lda     atemp+2         ; get 1's
        sta     msg+2           ; store it

        lda     day             ; get the day
        sta     divdnd+1        ; place to convert
        clr     divdnd          ; clear MSB
        jsr     bin2as          ; convert it
        lda     atemp+1         ; get 10's
        sta     msg+4           ; store it
        lda     atemp+2         ; get 1's
        sta     msg+5           ; store it

        lda     year            ; get the year
        sta     divdnd+1        ; place to convert
        clr     divdnd          ; clear MSB
        jsr     bin2as          ; convert it
        lda     atemp+1         ; get 10's
        sta     msg+7           ; store it
        lda     atemp+2         ; get 1's
        sta     msg+8           ; store it

        lda     hrs             ; get the hour
        sta     divdnd+1        ; place to convert
        clr     divdnd          ; clear MSB
        jsr     bin2as          ; convert it
        lda     atemp+1         ; get 10's
        sta     msg+10          ; store it
        lda     atemp+2         ; get 1's
        sta     msg+11          ; store it

        lda     mins            ; get the minutes
        sta     divdnd+1        ; place to convert
        clr     divdnd          ; clear MSB
        jsr     bin2as          ; convert it
        lda     atemp+1         ; get 10's
        sta     msg+13          ; store it
        lda     atemp+2         ; get 1's
        sta     msg+14          ; store it

stmsg:  ldhx    #msg            ; the string to write
        jsr     wrstr           ; go write it
        ldx     cntr            ; our field pointer
        clrh                    ; make sure H is 0
        lda     stcol,x         ; get the place to put the cursor
        jsr     wrcmd           ; place it

stscan: lda     curkey          ; get key pressed (if any)
        cmp     #kwait          ; see if wait code
        beq     stscan          ; no new data yet, wait
        cmp     #ksel           ; see if select
        beq     lmenus          ; if so, off to menus
        cmp     #knone          ; if no key, reset rapid count
        beq     strst           ; go do it
        cmp     #kup            ; see if up key
        beq     stupdn          ; go process
        cmp     #kdown          ; see if down key
        beq     stupdn          ; go do it if so

strst:  clr     keycnt          ; clear key pressed count
        bra     stscan          ; go wait for a key

lmenus: jmp     llmenu          ; long jump

stupdn: lda     #1              ; default inc/dec by 1
        sta     temp            ; remember it
        lda     keycnt          ; get key counter
        beq     stdoit          ; if 0 value, go inc/dec by 1
        cmp     #11             ; see if 11 samples
        blo     stkwt           ; if 1-11, no action
        dec     keycnt          ; don't want keycnt to overflow
        lda     #5              ; inc/dec by 5 when held down
        sta     temp            ; remember it

stdoit: lda     curkey          ; get keycode back again
        cmp     #kdown          ; see if down (vs up)
        beq     stdown          ; if so, go decrement time

stup:   ldx     cntr            ; get our counter
        clrh                    ; make sure H is 0
        ldx     stoff,x         ; point to real variable
        inc     year,x          ; get the field to increment
        lda     year,x          ; get incremented value
        cpx     #2              ; see if day of month
        bne     stupnf          ; if not, normal processing

        jsr     mthmax          ; get max days in current month
        sta     temp            ; save it
        ldx     cntr            ; get our counter again (X trashed)
        clrh                    ; make sure H is 0
        ldx     stoff,x         ; point to real variable
        lda     year,x          ; get it
        cmp     temp            ; do compare
        bra     stupfc          ; do checking

stupnf: cmp     stmax,x         ; compare to max value
stupfc: bls     stntim          ; if within limits, all done
        lda     stmin,x         ; otherwise roll, get min value
        sta     year,x          ; save as new value
        bra     stntim          ; all done

stdown: ldx     cntr            ; get our pointer
        ldx     stoff,x         ; point to real variable
        clrh                    ; make sure H is 0
        dec     year,x          ; get the field to increment
        lda     year,x          ; get incremented value
        bmi     stdnnn          ; if negative, we have underflowed
        cmp     stmin,x         ; compare to max value
        bhs     stntim          ; if within limits, all done
stdnnn: lda     stmax,x         ; otherwise roll, get min value
        sta     year,x          ; save as new value
        cpx     #2              ; see if day of month
        bne     stntim          ; if not, normal processing

        jsr     mthmax          ; get max days in current month
        ldx     cntr            ; get our counter again (X trashed)
        ldx     stoff,x         ; point to real variable
        clrh                    ; make sure H is 0
        sta     year,x          ; do compare

stntim: inc     keycnt          ; increment our key counter
        lda     #kwait          ; wait for next scan flag
        sta     curkey          ; store it
lstlp:  jmp     stlp            ; go display new time

stkwt:  inc     keycnt          ; increment key count
        lda     #kwait          ; wait for next scan flag
        sta     curkey          ; store it
        jmp     stscan          ; wait for next scan

llmenu: lda     curkey          ; get current keycode
        cmp     #knone          ; see if no key
        bne     llmenu          ; wait for no key
        inc     cntr            ; if select pressed, increment counter
        lda     cntr            ; get incremented value
        cmp     #6              ; see if done
        bne     lstlp           ; if not, loop back, else done

stoff:  db      1,2,0,3,4,7             ; Index offset for month,day,year,hour,minute,zulu
stmax:  db      99,12,31,23,59,0,0,23   ; Max values for year,month,day,hour,minute,zulu
stmin:  db      0,1,1,0,0,0,0,0         ; Min values for year,month,day,hour,minute,zulu
stcol:  db      $87,$8a,$8d,$90,$93,$90 ; Cursor positions for above

spcol:  db      $80,$81,$82,$83,$85,$86,$87,$89 ; Postions for 15 position string chars
        db      $8a,$8b,$8c,$8d,$8f,$90,$91
spmax:  db      '995999S1995999W'               ; Max values for each field (in ASCII)
spofs:  db      0,1,2,3,5,6,7,9,10,11,12,13,15,16,17 ; offsets into string


